package com.mine.modules.vm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mine.common.domain.query.PageQuery;
import com.mine.common.domain.vo.Result;
import com.mine.common.utils.AnnotationUtil;
import com.mine.modules.vm.entity.*;
import com.mine.modules.vm.mapper.VmSceneMapper;
import com.mine.modules.vm.service.IVmSceneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 场景菜单 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-23
 */
@Service
public class VmSceneServiceImpl extends ServiceImpl<VmSceneMapper, VmScene> implements IVmSceneService {

    @Override
    public Result<?> queryPageList(PageQuery pageQuery) {
        QueryWrapper<VmScene> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getKeywords())) {
            queryWrapper = AnnotationUtil.processCommonSearchKeywords(queryWrapper, VmScene.class, pageQuery.getKeywords());
        }
        Page<VmScene> page = new Page<>(pageQuery.getPageNo(), pageQuery.getPageSize());
        IPage<VmScene> pageList = this.page(page, queryWrapper);
        return Result.OK("查询成功！", pageList);
    }

    @Override
    public Result<?> addOrEdit(VmScene vmScene) {
        if (vmScene == null) {
            return Result.error("参数必填！");
        }
        if (StringUtils.isEmpty(vmScene.getId())) {
            if (this.save(vmScene)) {
                return Result.OK("添加成功！");
            } else {
                return Result.error("添加失败！");
            }
        } else {
            VmScene old = this.getById(vmScene.getId());
            if (old == null) {
                return Result.error("数据不存在，请刷新重试！");
            }
            if (this.updateById(vmScene)) {
                return Result.OK("修改成功！");
            } else {
                return Result.error("修改失败！");
            }
        }
    }

    @Override
    public Result<?> delete(String id) {
        this.removeById(id);
        return Result.OK("删除成功!");
    }

    @Override
    public Result<?> deleteBatch(String ids) {
        if(StringUtils.isEmpty(ids)){
            return Result.error("参数不完整！");
        }
        try {
            List<String> arr = Arrays.asList(ids.split(","));
            this.removeByIds(arr);
            return Result.OK("删除成功!");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("删除失败!");
        }
    }
}
